﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
namespace SchoolManagementSystem.Details
{
    public partial class Product_UnitChangeUC : DetailUC
    {
        public Product_UnitChangeUC()
        {
            InitializeComponent();
            this.Code = "ProductUnitChange";
            this.ObjectName = "quy đổi đơn vị";
            this.Type = new Product_UnitChange();

            comboboxUC_UnitFrom.Model = RegisterService.RegisterServices.ProductUnitService;
            comboboxUC_UnitFrom.Uc = new Product_UnitUC();
            comboboxUC_UnitFrom.ColumnMain = "Name";
            comboboxUC_UnitFrom.ColumnDisplay = "Code,Name";

            comboboxUC_UnitTo.Model = RegisterService.RegisterServices.ProductUnitService;
            comboboxUC_UnitTo.Uc = new Product_UnitUC();
            comboboxUC_UnitTo.ColumnMain = "Name";
            comboboxUC_UnitTo.ColumnDisplay = "Code,Name";

            

        }
        long UnitID;
        String Product_UnitChangeID = "1=1";
        protected override long DetailDisplay()
        {
            Product_UnitChange data = new Product_UnitChange();
            if (ID > 0)
            {
                data = (Product_UnitChange)RegisterService.RegisterServices.ProductUnitChangeService.GetByID(ID);
                Product_UnitChangeID = data.ID.ToString();
                comboboxUC_UnitFrom.ID = data.UnitID;
            }
            else
            {
                ID = 0;
                long ProductID = long.Parse(SearchString.Replace("'", "").Replace("ProductID=", "").Trim());
                var p = (Product)RegisterService.RegisterServices.ProductService.GetByID(ProductID);
                comboboxUC_UnitFrom.ID = p.Input_UnitID;
                data.Ratio = 1;
            }
            comboboxUC_UnitTo.ID = data.UnitChangeID;
            txtnumber.Text = data.Ratio.ToString();
            ckactive.Checked = data.Active;
            if (data.UnitID == data.UnitChangeID && ID > 0)
            {
                //comboboxUC_UnitTo.Enabled = false;
                //txtnumber.Enabled = false;
                commandBarUC1.cmdsave.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                //Delete command tren grid
            }
           
            return ID;
        }
        protected override long Save()
        {
            Product_UnitChange data = new Product_UnitChange();
            if (ID > 0)
            {
                data = (Product_UnitChange)RegisterService.RegisterServices.ProductUnitChangeService.GetByID(ID);
            }
            data = new Product_UnitChange();
            data.ID = ID;
            data.ProductID = long.Parse(SearchString.Replace("'", "").Replace("ProductID=", "").Trim());
            data.UnitChangeID = comboboxUC_UnitTo.ID;
            data.UnitID = comboboxUC_UnitFrom.ID;
            data.Ratio = int.Parse(txtnumber.Text);
            data.Active = ckactive.Checked;
            if (ID == 0)
            {
                if (!RegisterService.RegisterServices.ProductUnitChangeService.CheckDuplicateUnitChange(data))
                {
                    SchoolManagementSystem.Controls.MessageControl.ShowMessage("Bảng quy đổi đơn vị này đã tồn tại", "Warning", MessageControl.Icon.Warning);
                    return 0;
                }
                else
                {
                    ID = RegisterService.RegisterServices.ProductUnitChangeService.Save(data);
                }
            }
            else
            {
                ID = RegisterService.RegisterServices.ProductUnitChangeService.Save(data);
            }
            return ID;
        }
        public override bool Delete(long SelectedID)
        {
            return RegisterService.RegisterServices.ProductUnitChangeService.Delete(RegisterService.RegisterServices.ProductUnitChangeService.GetByID(SelectedID));
        }
        private void Product_UnitChangeUC_Load(object sender, EventArgs e)
        {
            AddTextChanged(groupControl1.Controls);
        }
    }
}
